package com.example.week8_mybatis_plus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.week8_mybatis_plus.entity.Goods;
import com.example.week8_mybatis_plus.entity.GoodsSuppliers;
import com.example.week8_mybatis_plus.entity.Supplier;
import com.example.week8_mybatis_plus.mapper.GoodsMapper;
import com.example.week8_mybatis_plus.mapper.GoodsSuppliersMapper;
import com.example.week8_mybatis_plus.mapper.SupplierMapper;
import com.example.week8_mybatis_plus.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangbo
 * @since 2022-10-25
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsSuppliersMapper goodsSuppliersMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Override
    public Page<Goods> getGoods(Map<String, Object> cond, Page page) {
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
        lqw.eq(cond.containsKey("name"), Goods::getName, cond.get("name"))
                .le(cond.containsKey("price_max"), Goods::getPrice, cond.get("price_max"))
                .ge(cond.containsKey("price_min"), Goods::getPrice, cond.get("price_min"))
                .eq(cond.containsKey("id"), Goods::getId, cond.get("id"));

        Page<Goods> res = goodsMapper.selectPage(page,lqw);

        List<Goods> lis = res.getRecords();

        for (Goods goods : lis) {
            getAndSetSuppliers(goods);
        }

        return res;
    }

    private void getAndSetSuppliers(Goods goods) {
        QueryWrapper<GoodsSuppliers> qw = new QueryWrapper<>();

        qw.eq("goods_id", goods.getId());

        List<GoodsSuppliers> list = goodsSuppliersMapper.selectList(qw);

        List<Supplier> suppliers = new LinkedList<>();

        for (GoodsSuppliers goodsSuppliers : list) {
            suppliers.add(supplierMapper.selectById(goodsSuppliers.getSuppliersId()));
        }
        goods.setSuppliers(suppliers);
    }

    @Override
    public Goods addGoods(Goods goods) {
        goods.setId(null);
        if (goodsMapper.insert(goods) > 0) {
            return goods;
        }
        return null;
    }

    @Override
    public Goods updateGoods(Goods goods) {
        if (goodsMapper.updateById(goods) > 0) {
            return goods;
        }
        return null;
    }

    @Override
    public boolean deleteGoods(int id) {
        return goodsMapper.deleteById(id) > 0;
    }
}
